package org.dfl.plugin.editor.actions;

import org.dfl.messages.Messages;
import org.dfl.plugin.DflPlugin;
import org.dfl.plugin.editor.model.DflDiagram;
import org.dfl.plugin.utils.WorkbenchUtil;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.WorkbenchException;

/**
 * run the flow action
 * @author Piotr Wlodarczyk
 *
 */
public class RunAction extends org.eclipse.gef.ui.actions.SelectionAction {
	public static final String ACTION_ID = "RunDfl"; //$NON-NLS-1$
	private DflDiagram diagram;

	public RunAction(IWorkbenchPart part, DflDiagram diagram) {
		super(part);
		this.diagram = diagram;
		setId(ACTION_ID);
		setImageDescriptor(DflPlugin.getImageDescriptor("icons/eclipse/run.gif")); //$NON-NLS-1$
		setHoverImageDescriptor(getImageDescriptor());
		setText(Messages.RunAction_name);
		setToolTipText(Messages.RunAction_tooltip);
	}

	protected boolean calculateEnabled() {
		if(diagram == null) 
			return false;
		if(diagram.isRunnning()){
			return false;
		}
		
		return true;
//		always can be run
//		return diagram.allTranisitonsWellDefined();
	}

	public void run() {
		// change the perspective
		try {
			WorkbenchUtil.getActiveWindow().getWorkbench().showPerspective(
					"org.dfl.plugin.perspectives.debugperspective",
					WorkbenchUtil.getActiveWindow());
		} catch (WorkbenchException e) {
		}

		// set tool to select tool
		diagram.getEditor().resetTool();
		diagram.setEditable(false);
		
		// run it
		diagram.run();
	}
}
